package com.feibi.imember_printer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.flutter.plugin.common.MethodChannel;
import print.Print;

public class TP809Util {
    Print Printer = new Print();

    public void connect(Context context, String strIP, String strPort, MethodChannel.Result result) {
        if (strIP.length() == 0) {
            result.error("400", "ip地址為空", null);
            return;
        }
        try {
            if (Printer != null) Printer.PortClose();
        } catch (Exception e) {
            Log.d("SDKSample", (new StringBuilder("Activity_Wifi --> onClickConnect ")).append(e.getMessage()).toString());
            result.error("400", "連接失敗:" + e.getMessage(), null);
            return;
        }


        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    if (Printer.PortOpen(context, "WiFi," + strIP + "," + strPort) != 0) {
                        Printer = null;
                        fail(result, "401", "連接失敗");
                    } else {
                        success(result);
                    }
                } catch (Exception e) {
                    fail(result, "40w", "失敗：" + e.getMessage());
                }
            }
        }.start();
    }

    public void disconnect(MethodChannel.Result result) {
        if (!Print.IsOpened()) {
            result.error("400", "請先連接打印機", "");
            return;
        }
        try {
            Print.PortClose();
            success(result);
            return;
        } catch (Exception e) {
            fail(result, "400", "失敗：" + e.getMessage());
            Log.e("SDKSample", (new StringBuilder("Activity_Main --> onClickClose ")).append(e.getMessage()).toString());
        }
    }

    public void isConnect(MethodChannel.Result result) {
        result.success(Print.IsOpened());
    }

    public void printText(String text, MethodChannel.Result result) {
        if (!Print.IsOpened()) {
            result.error("400", "請先連接打印機", "");
            return;
        }
        try {
//            Print.LanguageEncode = "gb2312";
            Print.LanguageEncode = "big5";
            Print.SetCharacterSet((byte) 0);
            Print.PrintText(text);
            Print.CutPaper(1, 10);
            success(result);
        } catch (Exception e) {
            e.printStackTrace();
            fail(result, "400", "失敗：" + e.getMessage());
        }
    }

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    public void printImage(String filePath, int size, MethodChannel.Result result) {
        if (!Print.IsOpened()) {
            result.error("400", "請先連接打印機", "");
            return;
        }

        try {
            Bitmap bmp = BitmapFactory.decodeFile(filePath);
            if (size != 0)
                bmp = Utility.Tobitmap(bmp, size, Utility.getHeight(size, bmp.getWidth(), bmp.getHeight()));
            Print.PrintBitmap(bmp, 1, 0);
            bmp.recycle();
            Print.CutPaper(1, 10);
            success(result);
        } catch (Exception e) {
            e.printStackTrace();
            fail(result, "400", "失敗：" + e.getMessage());
        }
//        executorService.execute(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
    }

    ///mode
    // 0 - 打開1號錢箱
    // 1 - 打開2號錢箱
    // 2 - 打開1號和2號錢箱
    public void openCashDrawer(int mode, MethodChannel.Result result) {
        if (!Print.IsOpened()) {
            result.error("400", "請先連接打印機", "");
            return;
        }
        try {
            int iRtn = Print.OpenCashdrawer(mode);
            if (iRtn == 0)
                success(result);
            else
                fail(result, "400", "失敗：" + iRtn);
        } catch (Exception e) {
            fail(result, "400", "失敗：" + e.getMessage());
            Log.d("SDKSample", (new StringBuilder("Activity_Cashdrawer --> onClickOpen1 ")).append(e.getMessage()).toString());
        }
    }


    public void success(MethodChannel.Result result) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                result.success(true);
            }
        });
    }

    public void fail(MethodChannel.Result result, String code, String message) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                result.error(code, message, "");
            }
        });
    }
}
